const oicq = require("oicq");
const qaData = require("../pro/QA/index.js")
const sj = require("../pro/sj/index.js")
const fs = require("fs");








function getMemberData() {
    return JSON.parse(fs.readFileSync("../resource/member.json").toString())
}

function saveMenberData(str) {
    fs.writeFileSync("../resource/member.json", str)
}

const func = (props) => {
    let {account, config} = props;
    let bot = oicq.createClient(account, config);
    const { segment, cqcode} = oicq;

    async function analysisMenverList(props) {
        let {group_ids} = props;
        if(group_ids.length > 0) {
            let menberData = getMemberData();
            group_ids.forEach(async group_id => {
                let menberList = await bot.getGroupMemberList(group_id)
                if(menberList) {
                    menberList.forEach(menber => {
                    })
                }
            })

        }
    }
    bot.on("system.online", async ()=>{
        
        // const gfs = await bot.acquireGfs(977610373) //传群号进入一个群文件系统
        // let paths = await gfs.ls()
        // console.log( paths ); 

        // let menberList = await bot.getGroupMemberList(684854899)
        // console.log(menberList);

        analysisMenverList({group_ids:[]})
        // let cqCom = cqcode.json(JSON.stringify({
        //     "app": "com.tencent.structmsg",
        //     "desc": "新闻",
        //     "view": "news",
        //     "ver": "0.0.0.1",
        //     "prompt": "&#91;分享&#93;彦祖，你那点赞的姿势真的很帅！&#91;doge&#93; ",
        //     "meta": {
        //         "news": {
        //             "action": "",
        //             "android_pkg_name": "",
        //             "app_type": 1,
        //             "appid": 107951776,
        //             "desc": "雪乃daisuki",
        //             "jumpUrl": "https:\\/\\/b23.tv\\/IqJMVR?share_medium=android&share_source=qq&bbid=XX716237C3995B24F16B33552A56AE0872243&ts=1634622488171",
        //             "preview": "https:\\/\\/external-30160.picsz.qpic.cn\\/44216b33cfd33d570cf1cc22355e4431\\/jpg1",
        //             "source_icon": "",
        //             "source_url": "",
        //             "tag": "哔哩哔哩",
        //             "title": "彦祖，你那点赞的姿势真的很帅！&#91;doge&#93; "
        //         }
        //     },
        //     "config": {
        //         "autosize": true,
        //         "ctime": 1634622499,
        //         "forward": true,
        //         "token": "818a06dfacabb8210a370d730700de7e",
        //         "type": "normal"
        //     }
        // })).replace(/\\\\/ig, "\\").replace(/amp;#/ig,"#");
        
        // bot.sendGroupMsg(747029011, cqCom)

        {/* <picture cover="http://read.html5.qq.com/image?src=share&t=0&imageUrl=[http://127.0.0.1:/3000/ip.html]" w="0" h="0" /> */}
        // <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
        // <msg serviceID="1" templateID="1" action="web" brief="[分享] 【1】" sourcePublicUin="" sourceMsgId="0" url="【4】" flag="0" sType="">
        //     <item layout="2">
        //         <picture cover="【3】" />
        //         <title>【1】</title>
        //         <summary>【2】</summary>
        //     </item>
        //     <source name="QQ浏览器" icon="http://url.cn/PWkhNu" url="http://url.cn/UQoBHn" action="app" a_actionData="com.tencent.mtt://【4】" i_actionData="tencent100446242://【4】" appid="-1" />
        // </msg>


    });
    bot.on("message.group", (data)=>{
        let {group_id, message, user_id, sender} = data;
        qaData({bot, data})
        sj({bot, data})
        if(group_id == 1061498138) {  // ☁️y☁️
            yuanshenShanghai(props)
        }
        if(group_id == 747029011) {  // ☁️y☁️
            
            console.log(data.message);

        }
        // if(group_id == 368074699 && bot.gl.has(368074699)) {
        //     qaData.forEach(item => {
        //         if(message[0].type=="text" && item.q(message[0].data.text)) {
        //             data.reply(item.a)
        //         }
        //     })
        // }
    });
    
    bot.on("notice.group.recall", data => {
        console.log("撤回============", data);
    })
  
    return bot
}

module.exports = func






// let {group_id, group_name, post_type, sender, user_id, message_id, message} = data
//     if(post_type == "message") {
//         if(group_id == 1061498138) {  // ☁️y☁️
//             yuanshenShanghai(props)
//         }

//         if(group_id == 980638292) {  // 惠二群
//             // bot.deleteMsg(id) //撤回
//             // bot.setGroupKick(gid, uid) //踢人
//             // bot.setGroupBan(gid, uid, 3600) //禁言
//             yuanshenShanghai(props)
//             rongyaoChehui(props)

//         }

//         if(group_id == 368074699) {  // sai
//             yuanshenShanghai(props)
            

//         }
//     }



function yuanshenShanghai(props) {
    let {bot, data} = props;
    let {group_id, group_name, post_type, sender, user_id, message_id, message} = data

    if(/ybl (\W+) (.+)$/ig.test(message)){
        let $1 = RegExp.$1.trim();
        let $2 = RegExp.$2.trim();
        if($1 == "计算原神伤害") {
            let tmp = {};
            $2.split(" ").forEach(v => {
                let tmpv = v.split("=")
                tmp[tmpv[0]] = tmpv[1]-0
            })
            let ret = getDmg(tmp)
            let retstr = `暴击期望伤害为${ret.qw} 暴击伤害为${ret.bj}`
            data.reply(`[CQ:at,qq=${user_id},text=@${sender.card}] ${retstr}`)
        }
    }
}

function rongyaoChehui(props) {
    let {bot, data} = props;
    let {group_id, group_name, post_type, sender, user_id, message_id, message} = data

    if(/复制这段王者密语/ig.test(message)){
        bot.deleteMsg(message_id) //撤回
        bot.setGroupBan(group_id, user_id, 60) //禁言
    }
}



function getDmg(props){
    let {攻击, 暴击, 爆伤, 增伤, rate, rea} = props
    if(!暴击) {暴击 = 5}
    if(!爆伤) {爆伤 = 50}
    if(!rate){rate = 1}
    if(!增伤) {增伤 = 0}
    if(!暴击 > 100){暴击 = 100}
    let baseat = 攻击 || 0;
    let bdmg = baseat * (1 + 爆伤 / 100);
    let bdps = (暴击 * 10 * bdmg + (1000 - 暴击 * 10) * baseat) / 1000;
    let adddmg = bdps * (1 + 增伤 / 100)
    let adddmgBj = bdmg * (1 + 增伤 / 100)
    if(rea){
        let {reaction, reactionQ, reactionDmgAdd} = rea
        let tmp = 1 + reactionDmgAdd + (278*reaction/(reaction+1400)) / 100;
        return {
            qw: adddmg * rate * reactionQ * tmp,
            bj: adddmgBj * rate * reactionQ * tmp
        }
    }
    return {
        qw: adddmg * rate,
        bj: adddmgBj * rate
    }
}
    // {
    //     self_id: 1130342837,
    //     time: 1633761112,
    //     post_type: 'message',
    //     message_type: 'group',
    //     sub_type: 'normal',
    //     message_id: 'P0UtGp4x9FUAAl+O458+FmFhN1gB',
    //     group_id: 1061498138,      // q群号
    //     group_name: '☁️原神☁️',
    //     user_id: 2654073941,
    //     anonymous: null,
    //     message: '1',
    //     raw_message: '1',
    //     atme: false,
    //     block: false,
    //     seqid: 155534,
    //     font: '微软雅黑',
    //     sender: {
    //       user_id: 2654073941,
    //       nickname: '神仙',
    //       card: '神仙',
    //       sex: 'male',
    //       age: 3,
    //       area: '加纳',
    //       level: 4,
    //       role: 'owner',
    //       title: ''
    //     },
    //     reply: [Function]
    //   }